package com.kdtech.analyse.video;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

/**
 * <电视台 - 安庆网络广播电视> 解析类
 *
 * @author KK
 *
 */
public class CztvAnalyse implements AnalyseNews {



	
	public boolean isDetailPage(String url) {
		String[] regex = {
					"http://www.cztv.cc/web_video/dt[0-9]*.asp\\?docid=[0-9]*",
				"http://www.cztv.cc/web_video/dt[0-9]*.asp\\?DocID=[0-9]*",
				};

		return RegexUtils.matchAny(url,regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();

		String title = null;
		Long date = null;
		String desc = null;
		String dateStr = null;
		String commnetNum = null;
		String clickNum = null;
		String updateUrl = null;
		Document doc = Jsoup.parse(htmltxt);

		title =   doc.select("td.td_right_profile div.cen").text();
		dateStr = doc.select("td.td_right_profile div.titleinfo").text();
//		desc =    doc.select("div#content2 div#gjnr div#gjnr_right div#Content").text();
//		clickNum =  doc.select("div#CNT div.box div.news_content p#acticle_info").text();
//		updateUrl = doc.select("div.left_center2 > div.article div.sj_font > p > script").attr("src");
		if (StringUtils.isNullOrEmpty(title)){
		}

		date = DateUtils.matchDate(dateStr);
			NewsMeta news = new NewsMeta();
			news.setUrl(url);
			news.setType(2);
			news.setTitle(StringUtils.trimSpace(title));
			news.setCommentNum(NumberUtils.parseInt(commnetNum));
			news.setClickNum(NumberUtils.parseInt(clickNum));

			news.setDate(date);
			news.setContent(StringUtils.trimSpace(desc));
			if (StringUtils.isNotBlank(updateUrl)){
				news.setUpdateUrl(updateUrl);
				Update(news);
			}
			return news;

	}

	
	public NewsMeta Update(NewsMeta meta) {
		if (StringUtils.isNotBlank(meta.getUpdateUrl())) {
			String html = CrawlHTML.GetHtml(meta.getUpdateUrl(), "UTF-8");
			if(StringUtils.isNotBlank(html)){
				String clickNumStr = StringUtils.substringBetween(html, "document.write('","');") ;
				Integer clickNum = NumberUtils.getInteger(clickNumStr);
				meta.setClickNum(clickNum);
				return meta;
			}
		}
		return null;
	}

	

	
}
